#include"Library_data_achieve.h"
#include <strings.h>
#include <string.h>
#include <fstream>

using namespace std;
using namespace LB;

void Library_data_achieve::loadbook(vector<Classbook> &v_classbooks)
{
	ifstream in;
	in.open("./Library/books.dat",ios::binary);
	char name[20] = {};
	int id;
	unsigned int booknum = 0;
	
	while( in.read((char*)&id,sizeof(int)) )
	{
		
		in.read((char*)&name,sizeof(name));
		in.read((char*)&booknum,sizeof(booknum));

		vector<Book> books;
		for(unsigned int i=0;i<booknum;i++)
		{
			
			int bookid;
			char bookname[20] = {};
			bool at_library;
			int restday;
			int borrowid;
			int orderid;
			float praise_val;
			int borrow_times;
			in.read((char*)&bookid,sizeof(int));
			in.read((char*)&(bookname),sizeof(bookname));
			in.read((char*)&(at_library),sizeof(bool));
			in.read((char*)&(restday),sizeof(int));
			in.read((char*)&(borrowid),sizeof(int));
			in.read((char*)&(orderid),sizeof(int));
			in.read((char*)&(praise_val),sizeof(float));
			in.read((char*)&(borrow_times),sizeof(int));

			unsigned int cmtnum = 0;
			in.read((char*)&cmtnum,sizeof(cmtnum));
 		
			vector<string> v_comment;
			
			for (unsigned int k = 0; k < cmtnum; ++k)
			{
				char comment[60] = {};
				in.read((char*)&(comment),sizeof(comment));
				v_comment.push_back(comment);
			}
			Book b(bookid,bookname,at_library,restday,borrowid,orderid,
				   praise_val,borrow_times,v_comment);

			books.push_back(b);
			
		}
		
		Classbook c(books,name,id);
		v_classbooks.push_back(c);
	}

	in.close();
}
void Library_data_achieve::savebook(vector<Classbook> &v_classbooks)
{
	ofstream out;
	out.open("./Library/books.dat",ios::binary);
	
	for(unsigned int i=0;i<v_classbooks.size();i++)
	{		
		out.write((char*)&v_classbooks[i].m_cid,sizeof(int));
		out.write((char*)&v_classbooks[i].m_classname,sizeof(v_classbooks[i].m_classname));
		
		unsigned int booknum=v_classbooks[i].v_book.size();
		out.write((char*)&booknum,sizeof(booknum));
		
		for(unsigned int j=0;j<booknum;j++)
		{
			out.write((char*)&(v_classbooks[i].v_book[j].m_bid),sizeof(int));
			out.write((char*)&(v_classbooks[i].v_book[j].m_bookname),sizeof(v_classbooks[i].v_book[j].m_bookname));
			out.write((char*)&(v_classbooks[i].v_book[j].at_library),sizeof(bool));
			out.write((char*)&(v_classbooks[i].v_book[j].restday),sizeof(int));
			out.write((char*)&(v_classbooks[i].v_book[j].borrowid),sizeof(int));
			out.write((char*)&(v_classbooks[i].v_book[j].orderid),sizeof(int));
			out.write((char*)&(v_classbooks[i].v_book[j].praise_val),sizeof(float));
			out.write((char*)&(v_classbooks[i].v_book[j].borrow_times),sizeof(int));

			unsigned int cmtnum = v_classbooks[i].v_book[j].v_comment.size();
			out.write((char*)&cmtnum,sizeof(cmtnum));
			
			
			for (unsigned int k = 0; k < cmtnum; ++k)
			{
				char buf[60] = {};
				strcpy(buf,v_classbooks[i].v_book[j].v_comment[k].c_str());
				out.write((char*)&buf,sizeof(buf));
			}
		}
		
	}
	out.close();
}
void Library_data_achieve::loadusers(vector<User>& v_users) 
{

	ifstream in("./Library/users.txt",ios::binary);
	unsigned int usernum =0;
	in>>usernum;
	int uid;
	char username[20];
	char password[20];
	float balance;
	map<int,char*> book;
	unsigned int booknum = 0;
	int key;
	
	for (unsigned int i = 0; i < usernum; i++)
	{
		char* bookname=new char[20];
		in>>uid>>username>>password>>balance;
		in>>booknum;
		
		for(unsigned int i=0;i<booknum;i++)
		{
			in>>key>>bookname;
			book.insert(make_pair(key,bookname));
		}
		
		User user(uid,username,password,balance,book);
		v_users.push_back(user);
	}
	

}
void Library_data_achieve::saveusers(vector<User>& v_users) 
{
	ofstream out("./Library/users.txt",ios::binary);
	unsigned int usernum=v_users.size();
	out<<usernum<<" ";
	for( unsigned int i=0; i<v_users.size(); i++ )
	{
		out<<v_users[i].m_uid<<" "<<v_users[i].m_username<<
		" "<<v_users[i].m_password<<" "<<v_users[i].balance<<endl;
		map<int,char*>::iterator p=v_users[i].mybook.begin();
		unsigned int num=v_users[i].mybook.size();
		out<<num<<endl;
		for(;p!=v_users[i].mybook.end();p++)
		{
			out<<p->first<<" "<<p->second<<endl;
		}

		
	}
}